#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <getch.h>
#include"network.h"
struct NetWork* nw;

void ftp_cd(struct NetWork* cd_nw,char* str)
{
	char buf[1024]={};
	int i=0;
	while('\0'!=str[i+3])
	{
		//printf("%c\n",str[i+3]);
		buf[i]=str[i+3];
		i++;
	}
	buf[i]='\0';
	char str1[1024]={};
	sprintf(str1,"CWD %s\n",buf);
	nsend(cd_nw,str1,strlen(str1));
	bzero(buf,sizeof(buf));
	nrecv(cd_nw,buf,sizeof(buf));
	puts(buf);
	sprintf(buf,"PWD\n");
	nsend(cd_nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(cd_nw,buf,sizeof(buf));
	puts(buf);

	sprintf(buf,"PASV\n");
	nsend(cd_nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(cd_nw,buf,sizeof(buf));
	puts(buf);

	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	sscanf(strchr(buf,'(')+1,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
	sprintf(buf,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4);

	NetWork* data_nw =open_network('c',SOCK_STREAM,buf,port1*256+port2);
	printf("connet success fd =%d\n",data_nw->fd);
	
	sprintf(buf,"LIST -al\n");
	nsend(cd_nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(cd_nw,buf,sizeof(buf));
	puts(buf);

	
	int ret =0i;
	bzero(str1,sizeof(str1));
	bzero(buf,sizeof(buf));
	while(ret = nrecv(data_nw,buf,sizeof(buf)))
	{
		printf("%s",buf);
		bzero(buf,sizeof(buf));
		}
	printf("\n");
	
	bzero(buf,sizeof(buf));
	nrecv(cd_nw,buf,sizeof(buf));
	puts(buf);
}
void ftp_gets(struct NetWork* gets_nw,char* str)
{
	char buf[1024]={};
	int i=0;
	while('\0'!=str[i+5])
	{
		printf("%c\n",str[i+5]);
		buf[i]=str[i+5];
		i++;
	}
	buf[i]='\0';
	char str1[50]={};
	sprintf(str1,"TYPE I\n");
	nsend(gets_nw,str1,strlen(str1));
	bzero(str1,sizeof(str1));
	nrecv(gets_nw,str1,sizeof(str1));
	puts(str1);
	
	sprintf(str1,"SIZE %s\n",buf);
	nsend(gets_nw,str1,strlen(str1));
	bzero(str1,sizeof(str1));
	nrecv(gets_nw,str1,sizeof(str1));
	puts(str1);
	
	sprintf(str1,"MDTM %s\n",buf);
	nsend(gets_nw,str1,strlen(str1));
	bzero(str1,sizeof(str1));
	nrecv(gets_nw,str1,sizeof(str1));
	puts(str1);
	
	sprintf(str1,"PASV \n");
	nsend(gets_nw,str1,strlen(str1));
	printf("^\n");	
	bzero(str1,sizeof(str1));
	nrecv(gets_nw,str1,sizeof(str1));
	printf("#\n");
	puts(str1);

	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	sscanf(strchr(str1,'(')+1,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
	sprintf(str1,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4);

	NetWork* data_nw =open_network('c',SOCK_STREAM,str1,port1*256+port2);
	printf("connet success fd =%d\n",data_nw->fd);

	sprintf(str1,"RETR %s\n",buf);
	nsend(gets_nw,str1,strlen(str1));
	bzero(str1,sizeof(str1));
	nrecv(gets_nw,str1,sizeof(str1));
	
	int ret =0;
	sprintf(str1,"./date/%s",buf);
	bzero(buf,sizeof(buf));
	int fd1=open(str1,O_WRONLY|O_CREAT|O_EXCL,0644);
	while(ret = nrecv(data_nw,buf,sizeof(buf)))
	{
		write(fd1,buf,strlen(buf)+1);
		bzero(buf,sizeof(buf));
		}
		close(fd1);
	printf("\n");

	bzero(str1,sizeof(str1));
	nrecv(gets_nw,str1,sizeof(str1));
}
void ftp_puts(NetWork* puts_nw,char* str)
{
	char buf[1024]={};
	int i=0;
	while('\0'!=str[i+5])
	{
		printf("%c\n",str[i+5]);
		buf[i]=str[i+5];
		i++;
	}
	buf[i]='\0';
	char str1[50]={};
	sprintf(str1,"TYPE I\n");
	nsend(puts_nw,str1,strlen(str1));
	bzero(str1,sizeof(str1));
	nrecv(puts_nw,str1,sizeof(str1));
	puts(str1);
	
	sprintf(str1,"PASV \n");
	nsend(puts_nw,str1,strlen(str1));	
	bzero(str1,sizeof(str1));
	nrecv(puts_nw,str1,sizeof(str1));
	puts(str1);

	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	sscanf(strchr(str1,'(')+1,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
	sprintf(str1,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4);

	NetWork* data_nw =open_network('c',SOCK_STREAM,str1,port1*256+port2);
	printf("connet success fd =%d\n",data_nw->fd);

	sprintf(str1,"STOR %s\n",buf);
	nsend(puts_nw,str1,strlen(str1));
	bzero(str1,sizeof(str1));
	nrecv(puts_nw,str1,sizeof(str1));
	
	int ret =0;
	sprintf(str1,"./date/%s",buf);
	bzero(buf,sizeof(buf));
	int fd1=open(str1,O_RDONLY);
	if(0>fd1)
	{
		perror("open");
		return;
	}
	while(ret = read(fd1,buf,sizeof(buf)))
	{
		puts(buf);
		nsend(data_nw,buf,strlen(buf)+1);
		bzero(buf,sizeof(buf));
		}
		close(fd1);
	printf("\n");

	bzero(str1,sizeof(str1));
	nrecv(puts_nw,str1,sizeof(str1));
	puts(str1);
}

int ftp_init(char* ip)
{
	NetWork* nw = open_network('c',SOCK_STREAM,ip,21);
	if(NULL == nw)
	{
		printf("open network sock null!!");
		return -1;
		}
	char buf[256]={};
	nrecv(nw,buf,sizeof(buf));
	puts(buf);

	char str[30]={};
	printf(">");
	gets(str);
	sprintf(buf,"USER %s\n",str);
	nsend(nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);
	

	printf(">");
	for(int i=0;;i++)
	{
		char c=getch();
		if(c == ' ')
		{
			str[i]='\0';
			break;
		}
		str[i]=c;
	}
	sprintf(buf,"PASS %s\n",str);
	nsend(nw,buf,strlen(buf));

	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);
	
	sprintf(buf,"SYST\n");
	nsend(nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);

	sprintf(buf,"OPTS UTF8 ON\n");
	nsend(nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);

	sprintf(buf,"PWD\n");
	nsend(nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);

	sprintf(buf,"PASV\n");
	nsend(nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);

	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	sscanf(strchr(buf,'(')+1,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
	sprintf(buf,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4);

	NetWork* data_nw =open_network('c',SOCK_STREAM,buf,port1*256+port2);
	printf("connet success fd =%d\n",data_nw->fd);
	
	sprintf(buf,"LIST -al\n");
	nsend(nw,buf,strlen(buf));
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);

	
	int ret =0;
	bzero(buf,sizeof(buf));
	while(ret = nrecv(data_nw,buf,sizeof(buf)))
	{
		printf("%s",buf);
		bzero(buf,sizeof(buf));
		}
	printf("\n");
	
	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	puts(buf);
	while(1)
	{
		printf(">");
		gets(str);
		if(0==strncmp("cd ",str,3))
		{
			printf("*\n");
			ftp_cd(nw,str);
		}
		else if(0==strncmp("gets ",str,5))
		{
			printf("*\n");
			ftp_gets(nw,str);
		}
		else if(0==strncmp("puts ",str,5))
		{
			ftp_puts(nw,str);
		}

	}
}

int main(int argc ,char* argv[])
{
	if(argc!=2)
	{
		printf("请输入正确的参数\n");
		return -1;
	}
	if(-1==ftp_init(argv[1]))
	{
		return 0;
	}
}


